package cn.fl.mcr.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.white.IMcrWhiteInspectService;
import cn.fl.mcr.vo.white.McrWhiteInspectVO;
import cn.lionbridgecapital.loan.model.base.Pager;
import cn.lionbridgecapital.loan.model.base.constant.BaseConstant;
import cn.lionbridgecapital.loan.model.condition.white.McrWhiteInspectQC;
import cn.lionbridgecapital.loan.model.dto.white.McrWhiteInspectDTO;
import cn.lionbridgecapital.loan.model.exception.BaseException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @Author: YangXin
 * @Description: 小贷线上白名单验证
 * @CreateTime: 2020-6-24-上午 11:41
 * @Version: 1.0.0
 **/
@Slf4j
@RestController
@RequestMapping("/mcr/whiteInspect")
public class McrWhiteInspectController extends BaseController {

    @Autowired
    private IMcrWhiteInspectService mcrWhiteInspectService;

    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = new ModelAndView("mcr/whiteInspect/whiteInspectListPage");
        log.info("[toListPage] 跳转到列表页面.usrName:{}", CurrentThreadContext.getCurrentUserName());
        return view;
    }

    /**
     * 初始化参数
     *
     * @param request           页面请求参数
     * @param mcrWhiteInspectQC 查询条件
     * @return 分页数据
     */
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object initList(HttpServletRequest request, McrWhiteInspectQC mcrWhiteInspectQC) {
        long startTm = System.currentTimeMillis();
        log.info("[initList] 初始化查询小贷线上白名单列表.开始时间.startTm:{}", startTm);
        Pager page = new Pager();

        // 获取参数
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        // 组装参数 - 分页
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        mcrWhiteInspectQC.setIsDel(BaseConstant.IS_YESNO_NO);
        page.setCondition(mcrWhiteInspectQC);
        ListPageVO<McrWhiteInspectVO> listPage = null;
        try {
            log.info("[initList] 初始化查询小贷线上白名单列表.分页请求param:{}", JSONObject.toJSONString(page));
            Pager pager = this.mcrWhiteInspectService.searchListPage(page);
            log.info("[initList] 初始化查询小贷线上白名单列表.分页响应结果result:{}", JSONObject.toJSONString(pager));
            List<McrWhiteInspectVO> resultList = (List<McrWhiteInspectVO>) pager.getResultList();
            listPage = new ListPageVO<>(resultList, pager.getTotalCount());
        } catch (Exception ex) {
            log.error("[initList] 初始化查询小贷线上白名单列表.分页请求.异常.ex{}", ex.getMessage(), ex);
        }
        long endTm = System.currentTimeMillis();
        log.info("[initList] 初始化查询小贷线上白名单列表.结束.共耗时:{}", (endTm - startTm));
        return listPage;
    }

    /**
     * 作废数据
     *
     * @param request 请求参数
     * @return 返回处理结果
     */
    @RequestMapping(value = "/removeById.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object removeById(HttpServletRequest request, HttpServletResponse response) {
        long startTm = System.currentTimeMillis();
        log.info("[removeById] 小贷线上白名单.作废数据.开始时间.startTm:{}", startTm);

        // 初始化参数
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "作废数据失败");

        // 获取参数
        String whiteId = request.getParameter("id");
        if (StringUtils.isEmpty(whiteId)) {
            log.error("[removeById] 小贷线上白名单.作废数据.请求ID为空,操作人{}", CurrentThreadContext.getCurrentUserName());
            dataResultVO.setInfo("请求ID为空");
            return dataResultVO;
        }
        try {
            McrWhiteInspectDTO mcrWhiteInspectDTO = new McrWhiteInspectDTO();
            mcrWhiteInspectDTO.setId(Long.parseLong(whiteId));
            mcrWhiteInspectDTO.setIsDel(BaseConstant.IS_YESNO_YES);
            boolean modifyWhite = this.mcrWhiteInspectService.modifyWhite(mcrWhiteInspectDTO);
            if (modifyWhite) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                dataResultVO.setInfo("作废数据成功");
            }
        } catch (Exception ex) {
            log.error("[removeById] 小贷线上白名单.作废数据.异常,ex:{}", ex.getMessage(), ex);
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo("作废数据异常");
        }
        long endTm = System.currentTimeMillis();
        log.info("[removeById] 小贷线上白名单.作废数据.结束.共耗时:{},result:{}", (endTm - startTm), JSONObject.toJSONString(dataResultVO));
        return dataResultVO;
    }

    /**
     * 置为无效
     *
     * @param request 请求参数
     * @return 返回处理结果
     */
    @RequestMapping(value = "/unValidById.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object validDataById(HttpServletRequest request, HttpServletResponse response) {
        long startTm = System.currentTimeMillis();
        log.info("[removeById] 小贷线上白名单.置为无效.开始时间.startTm:{},操作人:{}", startTm, CurrentThreadContext.getCurrentUserName());

        // 初始化参数
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "数据置为无效失败");

        // 获取参数
        String whiteId = request.getParameter("id");
        if (StringUtils.isEmpty(whiteId)) {
            log.error("[removeById] 小贷线上白名单.置为无效.请求ID为空,操作人{}", CurrentThreadContext.getCurrentUserName());
            dataResultVO.setInfo("请求ID为空");
            return dataResultVO;
        }
        try {
            McrWhiteInspectDTO mcrWhiteInspectDTO = new McrWhiteInspectDTO();
            mcrWhiteInspectDTO.setId(Long.parseLong(whiteId));
            mcrWhiteInspectDTO.setIsValid(BaseConstant.IS_YESNO_NO);
            boolean modifyWhite = this.mcrWhiteInspectService.modifyWhite(mcrWhiteInspectDTO);
            if (modifyWhite) {
                dataResultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                dataResultVO.setInfo("数据置为无效成功");
            }
        } catch (Exception ex) {
            log.error("[removeById] 小贷线上白名单.置为无效.异常,ex:{}", ex.getMessage(), ex);
            dataResultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            dataResultVO.setInfo("置为无效异常");
        }
        long endTm = System.currentTimeMillis();
        log.info("[removeById] 小贷线上白名单.置为无效.结束.共耗时:{}", (endTm - startTm));
        return dataResultVO;
    }

    /**
     * 跳转新增、修改页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toAdd.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object toAdd(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        ModelAndView view = new ModelAndView("mcr/whiteInspect/toAdd");
        if (StringUtils.isNotEmpty(id)){
            view.addObject("whiteId",id);
        }
        log.info("[toListPage] 跳转到新增页面.usrName:{},请求参数{}", CurrentThreadContext.getCurrentUserName(),id);
        return view;
    }

    /**
     * 新增数据
     * @return
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object add(HttpServletRequest request,McrWhiteInspectDTO mcrWhiteInspectDTO){
        long startTm = System.currentTimeMillis();
        log.info("[add] 小贷线上白名单.新增数据.开始时间.startTm:{},操作人:{}", startTm, CurrentThreadContext.getCurrentUserName());
        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "新增失败");
        if (Objects.isNull(mcrWhiteInspectDTO)){
            log.error("[add] 小贷线上白名单.新增数据.请求参数为空");
            resultVO.setInfo("请求参数为空");
            return resultVO;
        }
        try{
            log.info("[add] 小贷线上白名单.新增数据.请求参数.param{},操作人:{}",JSONObject.toJSONString(mcrWhiteInspectDTO), CurrentThreadContext.getCurrentUserName());
            Long aLong = this.mcrWhiteInspectService.insertWhite(mcrWhiteInspectDTO);
            log.info("[add] 小贷线上白名单.新增数据.响应参数.result{},操作人:{}",aLong, CurrentThreadContext.getCurrentUserName());
            if (aLong>0){
                resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                resultVO.setInfo("新增成功");
            }
        }catch (BaseException ex){
            log.error("[add] 小贷线上白名单.新增数据.base异常.ex:{}",ex.getMessage(),ex);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(ex.getMessage());
        }catch (Exception ex){
            log.error("[add] 小贷线上白名单.新增数据.异常.ex:{}",ex.getMessage(),ex);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("新增异常");
        }

        long endTm = System.currentTimeMillis();
        log.info("[add] 小贷线上白名单.新增数据.结束.共耗时:{},最终结果:{}", (endTm - startTm),JSONObject.toJSONString(resultVO));
        return resultVO;
    }

    /**
     * 修改数据
     * @return
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public Object modify(HttpServletRequest request,McrWhiteInspectDTO mcrWhiteInspectDTO){
        long startTm = System.currentTimeMillis();
        log.info("[add] 小贷线上白名单.修改数据.开始时间.startTm:{},操作人:{}", startTm, CurrentThreadContext.getCurrentUserName());
        DataResultVO resultVO = new DataResultVO(BaseConstant.IS_YESNO_NO, "修改失败");
        if (Objects.isNull(mcrWhiteInspectDTO)){
            log.error("[add] 小贷线上白名单.修改数据.请求参数为空");
            resultVO.setInfo("请求参数为空");
            return resultVO;
        }
        try{
            log.info("[add] 小贷线上白名单.修改数据.请求参数.param{},操作人:{}",JSONObject.toJSONString(mcrWhiteInspectDTO), CurrentThreadContext.getCurrentUserName());
            boolean modifyWhite = this.mcrWhiteInspectService.modifyWhite(mcrWhiteInspectDTO);
            log.info("[add] 小贷线上白名单.修改数据.响应参数.result{},操作人:{}",modifyWhite, CurrentThreadContext.getCurrentUserName());
            if (modifyWhite){
                resultVO.setSuccess(BaseConstant.IS_YESNO_YES);
                resultVO.setInfo("修改成功");
            }
        }catch (BaseException ex){
            log.error("[add] 小贷线上白名单.修改数据.base异常.ex:{}",ex.getMessage(),ex);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo(ex.getMessage());
        }catch (Exception ex){
            log.error("[add] 小贷线上白名单.修改数据.异常.ex:{}",ex.getMessage(),ex);
            resultVO.setSuccess(BaseConstant.IS_YESNO_NO);
            resultVO.setInfo("修改异常");
        }

        long endTm = System.currentTimeMillis();
        log.info("[add] 小贷线上白名单.修改数据.结束.共耗时:{},最终结果:{}", (endTm - startTm),JSONObject.toJSONString(resultVO));
        return resultVO;
    }

}
